#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

void handler(int sig){
    cout<<"begin wait child...."<<endl;
    pid_t pid;
    while((pid = waitpid(-1,nullptr,WNOHANG)) >= 0){
        cout<<"success wait: "<<pid<<endl;
    }
    cout<<"wait child over!"<<endl;
}

int main(){

    //捕捉SIGCHLD
    struct sigaction act;
    act.sa_handler = &handler;
    sigaction(SIGCHLD,&act,nullptr);

    if( 0 == fork()){
        cout << "i am child, ppid: " << getppid() << " ,pid: " << getpid() <<endl;
        sleep(3);
        exit(0);
    }

    while(1){
        cout<<"i am father,pid: " << getpid() <<endl; 
        sleep(1);
    }
    
    return 0;
}